import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectClassDemo {
    public static void reflectNewInstance() {
        try {
            Class<?> c1 = Class.forName("Student");
            Student student = (Student) c1.newInstance();
            System.out.println("学生对象"+ student);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }
    public static void reflectPrivateConstructor() {
        try {
            Class<?> c1 = Class.forName("Student");
            Constructor<?> constructor = c1.getDeclaredConstructor(String.class,int.class);
            constructor.setAccessible(true);

            Student student1 = (Student)constructor.newInstance("zhangsan",19);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException();
        } catch (InstantiationException e) {
            throw new RuntimeException();
        } catch (IllegalAccessException e) {
            throw new RuntimeException();
        } catch (NoSuchMethodException e) {
            throw new RuntimeException();
        } catch (InvocationTargetException e) {
            throw new RuntimeException();
        }
    }
    public static void reflectPrivateField() {
        try {
            Class<?> c1 = Class.forName("Student");
            Field field = c1.getDeclaredField("name");
            field.setAccessible(true);

            Student student = (Student) c1.newInstance();
            field.set(student,"喜羊羊");
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public static void reflectPrivateMethod() {
        try {
            Class<?> c1 = Class.forName("Student");
            Method method = c1.getDeclaredMethod("function", String.class);

            method.setAccessible(true);
            Student student = (Student) c1.newInstance();
            method.invoke(student,"参数");
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException();
        }  catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        //reflectNewInstance();
        //reflectPrivateConstructor();
        //reflectPrivateField();
        reflectPrivateMethod();
    }
}
